﻿#define InstallerName "test"
#define InstallerVersion "0.0.1"
#define Publisher "zaoangod"
#define PublisherURL ""
#define AppSupportURL ""
#define AppUpdatesURL ""
#define LicenseFile ""
#define IconFile "./static/icon/icon.ico"
#define StaticDirectory "D:\workspace\project\inno-setup\static"

[setup]
AppId={{C210836F-767B-E63E-63F9-C71599560842}
AppName={#InstallerName}
AppVersion={#InstallerVersion}
//AppVerName={#MyAppName}
AppPublisher={#Publisher}
AppPublisherURL={#PublisherURL}
AppSupportURL={#AppSupportURL}
AppUpdatesURL={#AppUpdatesURL}
// 安装应用文件夹位置
DefaultDirName={autopf}\{#InstallerName}
// 许可文件
LicenseFile={#LicenseFile}
// 打包后输出路径
OutputDir={#SourcePath}
// 打包后输出名称
OutputBaseFilename={#InstallerName}
// 安装包图标
SetupIconFile={#IconFile}
// 压缩方式
Compression=lzma
SolidCompression=yes
// 执行权限
//PrivilegesRequired=admin
// 卸载显示名称
UninstallDisplayName={#InstallerName}
// 默认群组名称
DefaultGroupName={#InstallerName}
UninstallIconFile={#IconFile}
// 不显"示准备安装"界面
DisableReadyPage=yes
// 不显示"选择开始菜单文件夹"界面
DisableProgramGroupPage=yes
// 不显示"选择安装路径"界面
DisableDirPage=yes
// 允许在指定架构系统安装
ArchitecturesAllowed=x64
// 64位安装模式安装
ArchitecturesInstallIn64BitMode=x64

[Languages]
Name: "chinese"; MessagesFile: "static\language\ChineseSimplified.isl"

[Files]
Source: "static/dll/botva2.dll"; DestDir: "{tmp}"; Flags: IgnoreVersion RecurseSubDirs CreateAllSubDirs;
Source: "static/dll/InnoCallback.dll"; DestDir: "{tmp}"; Flags: IgnoreVersion RecurseSubDirs CreateAllSubDirs;
Source: "static/image/*"; DestDir: "{tmp}"; Flags: DontCopy SolidBreak NoCompression; Attribs: hidden system;

[Code]
var
    CurrentDPI: integer;            // 当前DPI
    AppLogoImage: LongInt;          // 应用Logo
    LocationImage: LongInt;         // 安装位置图片
    WindowBackgroundImage: LongInt; // 窗口背景
    InstallButton: HWND;            // 安装按钮
    MinButton: HWND;                // 最小化按钮
    CloseButton: HWND;              // 关闭按钮
    LocationButton: HWND;           // 安装位置
    LocationFont: TFont;            // 安装位置字体
    ChooseLocationButton: HWND;     // 选择位置

    CheckLicensePanel: TPanel;      // 许可面板
    CheckLicenseTitle: TLabel;      // 勾选许可标题
    CheckLicenseButton: HWND;       // 勾选用户协议
    CheckLicenseState: Boolean;     // 是否勾选许可
const
    TOOLBAR_HEIGHT = 40;                   // 工具栏高度
    WINDOW_BUTTON_WIDTH = TOOLBAR_HEIGHT;  // 窗口按钮宽度
    WINDOW_BUTTON_HEIGHT = TOOLBAR_HEIGHT; // 窗口按钮高度

    WINDOW_HEIGHT_TOP = 330;                                  // 窗口上部分高度
    WINDOW_HEIGHT_BOTTOM = 150;                               // 窗口下部分高度
    WINDOW_WIDTH = 700;                                       // 窗口宽度
    WINDOW_HEIGHT = WINDOW_HEIGHT_TOP + WINDOW_HEIGHT_BOTTOM; // 窗口高度

    //---------------------------------安装界面变量---------------------------------
    INSTALL_BUTTON_WIDTH = 200; // 安装按钮宽度
    INSTALL_BUTTON_HEIGHT = 50; // 安装按钮高度

    INSTALL_LOCATION_WIDTH = 480; // 安装位置宽度
    INSTALL_LOCATION_HEIGHT = 40; // 安装位置高度

    INSTALL_LOCATION_CHOOSE_WIDTH = 100; // 选择位置按钮宽度
    INSTALL_LOCATION_CHOOSE_HEIGHT = 40; // 选择位置按钮高度

    CHECK_LICENSE_WIDTH = 16; // 选择用户协议宽度
    CHECK_LICENSE_HEIGHT = 16; // 选择用户协议高度

    ButtonClickEventID = 1;

type
    TButtonEventProc = procedure(h: HWND);
    TPBProc = function(h: HWND; message, w_param, l_param: LongInt): LongInt;

function ImgLoad(h: hwnd; FileName: PAnsiChar; Left, Top, Width, Height: integer; Stretch, IsBkg: boolean): LongInt; external 'ImgLoad@files:botva2.dll STDCALL DelayLoad';
procedure ImgSetVisibility(Image: LongInt; Visible: boolean); external 'ImgSetVisibility@files:botva2.dll STDCALL DelayLoad';
procedure ImgApplyChange(h: hwnd); external 'ImgApplyChanges@files:botva2.dll STDCALL DelayLoad';
procedure ImgApplyChanges(h: hwnd); external 'ImgApplyChanges@files:botva2.dll STDCALL DelayLoad';

function ButtonCreate(hParent: hwnd; Left, Top, Width, Height: Integer; FileName: PAnsiChar; ShadowWidth: integer; IsCheckBtn: boolean): hwnd; external 'BtnCreate@files:botva2.dll STDCALL DelayLoad';
procedure ButtonSetText(h: HWND; Text: PAnsiChar);  external 'BtnSetText@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetFont(h: HWND; Font: Cardinal); external 'BtnSetFont@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetTextAlignment(h: HWND; HorIndent, VertIndent: integer; Alignment:DWORD); external 'BtnSetTextAlignment@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetEvent(h: HWND; EventID: Integer; Event: LongWord); external 'BtnSetEvent@files:botva2.dll STDCALL DelayLoad';
function  ButtonGetCheck(h: HWND): boolean; external 'BtnGetChecked@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetCheck(h: HWND; value: boolean); external 'BtnSetChecked@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetEnable(h: HWND; value: boolean); external 'BtnSetEnabled@{tmp}\botva2.dll STDCALL DelayLoad';

function ReleaseCapture(): LongInt; external 'ReleaseCapture@user32.dll STDCALL';
function WrapButtonCallback(Callback: TButtonEventProc; Count: integer) : LongWord; external 'wrapcallback@files:InnoCallback.dll STDCALL DelayLoad';

// 提取临时资源
procedure ExtractTempFile();
begin
    Log('提取临时资源开始...');
    ExtractTemporaryFile('logo.png');
    ExtractTemporaryFile('check.png');
    ExtractTemporaryFile('location.png');
    ExtractTemporaryFile('button_min.png');
    ExtractTemporaryFile('button_close.png');
    ExtractTemporaryFile('button_install.png');
    ExtractTemporaryFile('choose-location.png');
    ExtractTemporaryFile('background_welcome.png');
    Log('提取临时资源完成...');
end;

// 窗口拖动事件
procedure WizardMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
    ReleaseCapture();
    SendMessage(WizardForm.Handle, $0112, $F012, 0);
    Log('鼠标拖动')
end;

// 按钮事件
procedure ButtonEvent(h: hwnd; Callback: TButtonEventProc);
begin
    Log('按钮事件');
    ButtonSetEvent(h, 1, WrapButtonCallback(Callback, 1));
end;

// 触发安装事件
procedure event_trigger_install(button: HWND);
begin
    Log('触发安装事件');
    // WizardForm.NextButton.OnClick(WizardForm);
end;

// DPI缩放
function DPIScale(value: Integer): Integer;
begin
    Result := value * CurrentDPI / 1000;
end;

// Checkbox 切换
procedure CheckboxClick(button: HWND);
begin
    if ButtonGetCheck(button) = true then
    begin
        Log('正选')
        ButtonSetCheck(button, True);
    end else
    begin
        Log('反选')
        ButtonSetCheck(button, False);
    end;
end;

procedure InitializeWizard();
var
    ApplicationName,
    WindowTitle,
    LabelToMove,
    LabelWelcome: TLabel;
    WindowW: Integer;
    WindowH: Integer;
begin
    Log('InitializeWizard...');

    // 获取当前DPI
    CurrentDPI :=  1000 * WizardForm.Font.PixelsPerInch / 96;

    // 提取临时文件
    ExtractTempFile();
    //
    WizardForm.InnerNotebook.Hide();              // 隐藏内嵌的提示文字
    WizardForm.OuterNotebook.Hide();              // 隐藏外围的提示文字
    WizardForm.Bevel.Hide();                      // 隐藏上下分割线
    WizardForm.BorderStyle := bsNone;             // 隐藏边框
    WizardForm.Position := poScreenCenter;        // 屏幕中心
    WizardForm.Color := $f7f7f7;                  // 背景颜色
    WizardForm.Width := DPIScale(WINDOW_WIDTH);   // 宽度
    WizardForm.Height := DPIScale(WINDOW_HEIGHT); // 高度

    // ------------------------------------创建并且显示背景图------------------------------------
    WindowBackgroundImage := ImgLoad(WizardForm.Handle, ExpandConstant('{tmp}\background_welcome.png'), 0, 0, DPIScale(WINDOW_WIDTH), DPIScale(WINDOW_HEIGHT), True, True);
    ImgSetVisibility(WindowBackgroundImage, True);

    // 创建最小化和关闭按钮
    CloseButton := ButtonCreate(WizardForm.Handle, DPIScale(WINDOW_WIDTH) - DPIScale(WINDOW_BUTTON_WIDTH), 0, DPIScale(WINDOW_BUTTON_WIDTH), DPIScale(WINDOW_BUTTON_HEIGHT), ExpandConstant('{tmp}\button_close.png'), 0, False);
    MinButton := ButtonCreate(WizardForm.Handle, DPIScale(WINDOW_WIDTH) - (DPIScale(WINDOW_BUTTON_WIDTH) * 2), 0, DPIScale(WINDOW_BUTTON_WIDTH), DPIScale(WINDOW_BUTTON_HEIGHT), ExpandConstant('{tmp}\button_min.png'), 0, False);

    //
    CheckLicenseTitle := TLabel.Create(WizardForm);
    CheckLicenseTitle.Parent := WizardForm;
    CheckLicenseTitle.AutoSize := True;
    CheckLicenseTitle.Caption := '我同意用户许可';
    CheckLicenseTitle.Left := DPIScale(CHECK_LICENSE_WIDTH + 60);
    CheckLicenseTitle.Top := DPIScale(WINDOW_HEIGHT_TOP + 50 + INSTALL_LOCATION_HEIGHT + CHECK_LICENSE_HEIGHT);
    //CheckLicenseTitle.Font.Size := DPIScale(10);
    //CheckLicenseTitle.Font.Height := DPIScale(CHECK_LICENSE_HEIGHT);
    // ------------------------------------应用------------------------------------
    ImgApplyChanges(WizardForm.Handle);

    // 主页面版
    CheckLicensePanel := TPanel.Create(WizardForm);
    CheckLicensePanel.Parent := WizardForm;
    CheckLicensePanel.Left := 0;
    CheckLicensePanel.Top := 100;
    CheckLicensePanel.Width := DPIScale(WINDOW_WIDTH);
    CheckLicensePanel.Height := DPIScale(CHECK_LICENSE_HEIGHT);
    CheckLicensePanel.BorderWidth := 0;
    CheckLicensePanel.BevelKind := bkNone;
    CheckLicensePanel.BevelInner := bvNone;
    CheckLicensePanel.BevelOuter := bvNone;
    CheckLicensePanel.ParentBackground := True;

    // 协议面板
    CheckLicensePanel := TPanel.Create(WizardForm);
    CheckLicensePanel.Parent := WizardForm;
    CheckLicensePanel.Left := 0;
    CheckLicensePanel.Top := 100;
    CheckLicensePanel.Width := DPIScale(WINDOW_WIDTH);
    CheckLicensePanel.Height := DPIScale(CHECK_LICENSE_HEIGHT);
    CheckLicensePanel.BorderWidth := 0;
    CheckLicensePanel.BevelKind := bkNone;
    CheckLicensePanel.BevelInner := bvNone;
    CheckLicensePanel.BevelOuter := bvNone;
    CheckLicensePanel.ParentBackground := True;

end;

procedure CurPageChanged(CurPageID: Integer);
begin

    // 隐藏相关原始元素
    WizardForm.NextButton.Visible := False;
    WizardForm.BackButton.Visible := False;
    WizardForm.CancelButton.Visible := False;



    // 欢迎页面
    if (CurPageID = wpWelcome) then
    begin

        // 居左位置 = 窗口宽度/2 - 按钮宽度/2
        // 居上位置 = 窗口高度 - 按钮高度 - 50
        // ButtonCreate{
        //     Left,
        //     Top,
        //     Width,
        //     Height
        // }
        InstallButton := ButtonCreate(WizardForm.Handle, (DPIScale(WINDOW_WIDTH) / 2) - (DPIScale(INSTALL_BUTTON_WIDTH) / 2), (DPIScale(WINDOW_HEIGHT_TOP) - DPIScale(INSTALL_BUTTON_HEIGHT) - DPIScale(50)), DPIScale(INSTALL_BUTTON_WIDTH), DPIScale(INSTALL_BUTTON_HEIGHT), ExpandConstant('{tmp}\button_install.png'), 0, False);
        ButtonEvent(InstallButton, @event_trigger_install);

        // -----------------------------------创建并且显示安装和按钮位置-----------------------------------
        // 安装位置
        LocationButton := ButtonCreate(WizardForm.Handle, 60, DPIScale(WINDOW_HEIGHT_TOP + 50), DPIScale(INSTALL_LOCATION_WIDTH), DPIScale(INSTALL_LOCATION_HEIGHT), ExpandConstant('{tmp}\location.png') ,0 ,False);
        ButtonSetText(LocationButton, WizardForm.DirEdit.Text);
        LocationFont := TFont.Create;
        with LocationFont do begin
            Size := DPIScale(10);
        end;
        ButtonSetFont(LocationButton, LocationFont.Handle);
        ButtonSetTextAlignment(LocationButton, DPIScale(INSTALL_LOCATION_HEIGHT / 4), DPIScale(INSTALL_LOCATION_HEIGHT / 4), 0);
        // 选择按钮
        ChooseLocationButton := ButtonCreate(WizardForm.Handle, DPIScale(INSTALL_LOCATION_WIDTH + 60), DPIScale(WINDOW_HEIGHT_TOP + 50), DPIScale(INSTALL_LOCATION_CHOOSE_WIDTH), DPIScale(INSTALL_LOCATION_CHOOSE_HEIGHT), ExpandConstant('{tmp}\choose-location.png') ,0 ,False);

        // ------------------------------------勾选用户协议------------------------------------
        CheckLicenseButton := ButtonCreate(WizardForm.Handle, 60, DPIScale(WINDOW_HEIGHT_TOP + 50 + INSTALL_LOCATION_HEIGHT), DPIScale(CHECK_LICENSE_WIDTH), DPIScale(CHECK_LICENSE_HEIGHT), ExpandConstant('{tmp}\check.png'), 0, True);
        // ButtonEvent(CheckLicenseButton, ButtonClickEventID, WrapButtonCallback(@CheckboxClick, 1));
        ButtonEvent(CheckLicenseButton, @CheckboxClick);
        // ------------------------------------应用------------------------------------
        ImgApplyChanges(WizardForm.Handle);
    end;

    // 安装页面
    if (CurPageID = wpInstalling) then
    begin

    end;

    // 完成页面
    if (CurPageID = wpFinished) then
    begin

    end;
end;
